package com.future.mayi.fragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.GoodsListActivity;
import com.future.mayi.adapter.BrandAdapter;
import com.future.mayi.model.BrandModel;
import com.future.mayi.response.BrandListResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BrandFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    @BindView(R.id.grid_view)
    GridView gridView;

    private BrandAdapter adapter;

    private List<BrandModel> list;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_brand;
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        adapter = new BrandAdapter(getContext(), R.layout.layout_item_brand, list);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        new HttpRequest<BrandListResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.getBrandList)
                .setListener(new HttpRequest.OnNetworkListener<BrandListResponse>() {
                    @Override
                    public void onSuccess(BrandListResponse response) {
                        adapter.updateList(response.getList());
                    }

                    @Override
                    public void onFail(String message, BrandListResponse response) {

                    }
                }).start(new BrandListResponse());
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BrandModel model = (BrandModel) parent.getAdapter().getItem(position);
        if (model == null) return;
        GoodsListActivity.startActivity(getActivity(), GoodsListActivity.BrandStyle, model.getBrand_id());
    }
}
